package com.solome.net;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Net.HttpMethods;
import com.badlogic.gdx.Net.HttpRequest;
import com.badlogic.gdx.Net.HttpResponse;
import com.badlogic.gdx.Net.HttpResponseListener;
import com.solome.base.GameStatus;
import com.solome.base.Logger;

/**
 * @author soells
 * @E-mail:soells@foxmail.com
 * @version 创建时间：2014年12月3日 下午2:43:37
 * @DES 程序的简单说明
 */
public class HTTP {
	 private static String url = "http://127.0.0.1:16999";
	// private static String url = "http://192.168.1.9:8080";
	private static String session = "";

	public static void send(final ByteArray array, String methods,
			final NetCallback callback) {
		// 大多数情况下应该使用post请求...
		Logger.error("HTTP", "url=" + url);
		HttpRequest request = new HttpRequest(methods);
		request.setUrl(url);
		request.setTimeOut(5000);
		ByteArrayInputStream bais = null;
		DataInputStream stream = null;
		try {
			byte[] iData = array.Wrap();
			ByteArray byteArray = new ByteArray();
			// byteArray.PutShort(KMath.getIntValue(iData, 0, iData.length));
			byte[] data = byteArray.PutUTF(session)/* .PutShort(1).PutShort(1) */
			.PutShort(array.getCommandID()).PutShort(iData.length).Put(iData)
					.Wrap();
			array.close();
			byteArray.close();
			bais = new ByteArrayInputStream(data);
			stream = new DataInputStream(bais);
			request.setContent(stream, data.length);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (bais != null) {
					bais.close();
				}
				if (array != null) {
					array.close();
				}
				if (stream != null) {
					stream.close();
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		Gdx.net.sendHttpRequest(request, new HttpResponseListener() {
			GameStatus status = new GameStatus();

			@Override
			public void handleHttpResponse(HttpResponse httpResponse) {
				final InputStream resultAsStream = httpResponse
						.getResultAsStream();
				if (resultAsStream != null) {
					DataInputStream _dis = null;
					try {
						_dis = new DataInputStream(resultAsStream);
						// int q = 1;//data.readInt();
						// int w = 2;//data.readShort();
						// String s = data.readUTF();
						// String s2 = data.readUTF();
						// System.out.println("q=" + q + ",w=" + w + ",s=" + s
						// + ",s2=" + s2);
						_dis.readByte();
						int size = _dis.readInt();
						if (size < 0 || size > 1024 * 1024) {
							status.setMessage("数据错误.");
							status.setSuccess(false);
							callback.callback(status);
							return;
						}
						_dis.readShort();
						// for (int i = 0; i < count; i++) {
						int commandID = _dis.readUnsignedShort();
						_dis.readInt();
						status.setStatusCode(commandID);
						status.setMessage("成功");
						status.setSuccess(true);
						status.setBindingObject(_dis);
						callback.callback(status);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} finally {
						try {
							_dis.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				} else {
				}
				try {
				} finally {
					if (resultAsStream != null)
						try {
							resultAsStream.reset();
							resultAsStream.close();
						} catch (IOException e) {

						}
				}
			}

			@Override
			public void failed(Throwable t) {
				try {
					status.setMessage("网络连接失败.");
					status.setSuccess(false);
					callback.callback(status);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			@Override
			public void cancelled() {
				Gdx.app.log("HttpRequestExample", "cancelled");
			}
		});
	}

	public static void send(final ByteArray array, final NetCallback callback) {
		send(array, HttpMethods.POST, callback);
	}

	public static String getSession() {
		return session;
	}

	public static void setSession(String session) {
		HTTP.session = session;
	}
}
